package com.e1xp.deport_management_system.controller;


import com.e1xp.deport_management_system.entity.Rack;
import com.e1xp.deport_management_system.exception.CustomException;
import com.e1xp.deport_management_system.service.IRackService;
import com.e1xp.deport_management_system.utils.AjaxResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author E1xP
 * @since 2021-04-09
 */
@Controller
@RequestMapping("/rack")
public class RackController {

    @Resource
    IRackService rackService;

    /**
     * 用于渲染货架页面
     *
     * @param instanceId 实例id
     * @return ModelAndView
     */
    @RequestMapping("/rackData")
    public ModelAndView rackData(@SessionAttribute long instanceId) {
        ModelAndView modelAndView = new ModelAndView();
        List<Rack> rackList = rackService.getList(instanceId);
        modelAndView.addObject("rackList", rackList);
        modelAndView.setViewName("rackManager");
        return modelAndView;
    }

    /**
     * 用于渲染添加货架页面
     *
     * @return ModelAndView
     */
    @RequestMapping("/addRackPage")
    public ModelAndView addRackPage() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("addRack");
        return modelAndView;
    }

    /**
     * 用于添加
     *
     * @param instanceId 实例id
     * @param rack       货架实体
     * @return AjaxResponse
     * @throws CustomException 当实例不存在，货架名重复时抛出该异常
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResponse addRack(@SessionAttribute long instanceId, @RequestBody Rack rack) throws CustomException {
        rackService.add(instanceId, rack);
        return AjaxResponse.success();
    }

    /**
     * 用于删除货架
     *
     * @param instanceId 实例id
     * @param rackId     货架id
     * @return AjaxResponse
     * @throws CustomException 当实例不存在，货架不存在时抛出该异常
     */
    @RequestMapping("/delete")
    @ResponseBody
    public AjaxResponse delete(@SessionAttribute long instanceId, long rackId) throws CustomException {
        rackService.delete(instanceId, rackId);
        return AjaxResponse.success();
    }

    /**
     * 用于渲染货架详情页面
     *
     * @param instanceId 实例id
     * @param rackId     货架号
     * @return ModelAndView
     */
    @RequestMapping("/detail")
    public ModelAndView detail(@SessionAttribute long instanceId, long rackId) {
        ModelAndView modelAndView = new ModelAndView();
        Rack rack = rackService.get(instanceId, rackId);
        if (rack == null) {
            modelAndView.setViewName("redirect:/401Page.html");
        } else {
            modelAndView.addObject("rack", rack);
            modelAndView.setViewName("changeRack");
        }
        return modelAndView;
    }

    /**
     * 用于更新货架
     *
     * @param instanceId 实例id
     * @param rack       货架实体
     * @return AjaxResponse
     * @throws CustomException 当实例不存在，传入数据不完整时抛出该异常
     */
    @PostMapping("/update")
    @ResponseBody
    public AjaxResponse update(@SessionAttribute long instanceId, Rack rack) throws CustomException {
        rackService.update(instanceId, rack);
        return AjaxResponse.success();
    }

    /**
     * 用于获取货架信息
     *
     * @param instanceId 实例id
     * @return AjaxResponse
     */
    @PostMapping("/getSelection")
    @ResponseBody
    public AjaxResponse getSelection(@SessionAttribute long instanceId) {
        List<Map<String, String>> selectList = rackService.getSelection(instanceId);
        return AjaxResponse.success(selectList);
    }
}
